package com.gx.common.aspectj;


import com.alibaba.fastjson.JSON;
import com.google.common.net.HttpHeaders;
import com.gx.aspect.annotation.OperationLogger;
import com.gx.base.CurrentUser;
import com.gx.core.shiro.utils.ShiroUtils;
import com.gx.domain.SysLogOperation;
import com.gx.enums.OperationStatusEnum;
import com.gx.service.api.SysLogOperationService;
import com.gx.utils.DateUtils;
import com.gx.utils.HttpContextUtils;
import com.gx.utils.IpUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;


@Aspect
@Component
@Slf4j
public class SysLogOperationAspect {

    @Autowired
    private SysLogOperationService sysLogOperationService;

    @Pointcut("@annotation(com.gx.aspect.annotation.OperationLogger)")
    public void logPointCut() {

    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        try {
            //执行方法
            Object result = point.proceed();

            //执行时长(毫秒)
            long time = System.currentTimeMillis() - beginTime;
            //保存日志
            saveLog(point, time, OperationStatusEnum.SUCCESS.getCode());

            return result;
        } catch (Exception e) {
            //执行时长(毫秒)
            long time = System.currentTimeMillis() - beginTime;
            //保存日志
            saveLog(point, time, OperationStatusEnum.FAIL.getCode());

            throw e;
        }
    }

    private void saveLog(ProceedingJoinPoint joinPoint, long time, Integer status) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        SysLogOperation log = new SysLogOperation();
        OperationLogger annotation = method.getAnnotation(OperationLogger.class);
        if (annotation != null) {
            //注解上的描述
            log.setOperation(annotation.modelName() + " " + annotation.option());
        }

        // 注解时当前用户信息写入操作日志
        CurrentUser currentUser = ShiroUtils.getCurrentUser();
        if (currentUser != null) {
            log.setCreator(currentUser.getId());
            log.setCreatorName(currentUser.getUsername());
        }

        log.setCreateDate(DateUtils.getNowDate());

        log.setStatus(status);
        log.setRequestTime((int) time);

        //请求相关信息
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        log.setIp(IpUtils.getIpAddr(request));
        log.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT));
        log.setRequestUri(request.getRequestURI());
        log.setRequestMethod(request.getMethod());

        //请求参数
        Object[] args = joinPoint.getArgs();
        try {
            String params = JSON.toJSONString(args[0]);
            log.setRequestParams(params);
        } catch (Exception e) {

        }

        //保存到DB
        sysLogOperationService.addObject(log);
    }
}
